//
//  HESickbedDetailViewController.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/5/14.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HESickbedDetailViewController.h"
#import "HEConsulCommentViewController.h"

@interface HESickbedDetailViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) QMUIFillButton *consulBtn;

@end

@implementation HESickbedDetailViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
 
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"服务计划";
    self.view.backgroundColor = KWhiteColor;
    [self initUI];
}

- (void)initUI {
    
    // 1. 添加scrollView
    [self.view addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentView];
    
  
    UIView *headWarp = [UIView new];
    [self.contentView addSubview:headWarp];
    
    
     [headWarp mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(self.contentView).mas_offset(15);
         make.left.equalTo(self.contentView).mas_offset(15);
         make.right.equalTo(self.contentView).mas_offset(-15);
         
     }];
    
    NSMutableArray *smallArray = @[].mutableCopy;
    
    NSArray *items = @[@{@"title":@"服务项目：", @"value": nullToDef(_model.service_name)},
                       @{@"title":@"服务日期：", @"value": [NSString stringWithFormat:@"%@ %@", _model.service_date, _model.service_time]},
                       @{@"title":@"服务状态：", @"value": nullToDef(_model.KStatusText), @"textColor": _model.KStatusTextColor},
                       @{@"title":@"服务人员：", @"value": nullToDef(@" ")},
                    ];
    
    [items enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        UIView *smallItem = [self createItemViewWithModel:obj];
        [headWarp addSubview: smallItem];

        [smallArray addObject:smallItem];
        [smallItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headWarp);
            make.right.equalTo(headWarp);
            
            if (idx == 0) {
                make.top.equalTo(headWarp);
            }else {
                UIView *lastView = smallArray[idx-1];
                make.top.equalTo(lastView.mas_bottom).offset(10);
            }

            if (idx == (items.count-1)) {
                make.bottom.equalTo(headWarp);
            }
        }];
    }];
 
    if (_model.order_status >= HEOrderWelfareStatusWaitWelfare) {
        [self createServerNameView: headWarp];
    }
}

// 创建 小项视图 【剂量：1mg/次】
- (UIView *)createItemViewWithModel:(NSDictionary *)model{
    UIView *itemWarp = [UIView new];
 
    // middle
    UILabel *nameTitlLb = [UILabel new];
    nameTitlLb.text = model[@"title"];
    nameTitlLb.font = DYBlodFont(21);
    nameTitlLb.textColor = CNavLightBlackColor;
    [itemWarp addSubview: nameTitlLb];
    
    // middle
    UILabel *nameLb = [UILabel new];
    nameLb.text = model[@"value"];
    nameLb.font = DYFont(17);
    nameLb.textColor = CNavLightBlackColor;
    if ([model containsObjectForKey:@"textColor"]) {
        nameLb.textColor = model[@"textColor"];
    }
    [itemWarp addSubview: nameLb];
     
    [nameTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemWarp);
        make.left.equalTo(itemWarp);
    }];
    [nameTitlLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemWarp);
        make.left.equalTo(nameTitlLb.mas_right).offset(3);
        make.right.bottom.equalTo(itemWarp);
    }];
    
    return itemWarp;
}

- (void)createServerNameView:(UIView *)topView {
    
    UIView *topLine = [UIView new];
    topLine.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:topLine];
    
    
    // 头像
    UIImageView *iconView = [UIImageView new];
    [iconView sd_setImageWithURL:[NSURL URLWithString:_model.avatar] placeholderImage:IMAGE_NAMED(@"hmang")];
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    [iconView hg_setAllCornerWithCornerRadius: 40];
    [self.contentView addSubview:iconView];
    
    // 服务人员名称
    UILabel *nameTitlLb = [UILabel new];
    nameTitlLb.text = _model.service_username;
    nameTitlLb.font = DYBlodFont(18);
    nameTitlLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview: nameTitlLb];
    
    // 电话
    UILabel *phoneTitlLb = [UILabel new];
    phoneTitlLb.text = @"电话：";
    phoneTitlLb.font = DYBlodFont(17);
    phoneTitlLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview: phoneTitlLb];
     
    UILabel *phoneLb = [UILabel new];
    phoneLb.text = _model.phone;
    phoneLb.font = DYBlodFont(17);
    phoneLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview: phoneLb];
    
    // 电话
    UIImageView *phoneView = [UIImageView new];
    phoneView.image = IMAGE_NAMED(@"call_main");
    phoneView.contentMode = UIViewContentModeScaleAspectFill;
    phoneView.userInteractionEnabled = YES;
    [self.contentView addSubview:phoneView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callPhoneClick)];
    [phoneView addGestureRecognizer:tap];
    
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = CSplitLineColor;
    [self.contentView addSubview:bottomLine];
    
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(20);
        make.left.right.equalTo(self.contentView);
        make.height.mas_offset(1);
    }];

    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLine.mas_bottom).mas_offset(15);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.height.mas_offset(80);
    }];
    
    [nameTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_top);
        make.left.equalTo(iconView.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [phoneTitlLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(iconView.mas_bottom);
        make.left.equalTo(iconView.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
     
    [phoneLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneTitlLb.mas_top);
        make.left.equalTo(phoneTitlLb.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(iconView);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.width.height.mas_offset(25);
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconView.mas_bottom).offset(15);
        make.left.right.equalTo(self.contentView);
        make.height.mas_offset(1);
    }];
    
    if (_model.order_status == HEOrderWelfareStatusFinish) {
        [self creatCommentBtn: bottomLine];
    }
}

- (void)creatCommentBtn:(UIView *)topView {
    QMUIFillButton *consulBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    _consulBtn = consulBtn;
    consulBtn.titleLabel.font = DYBlodFont(15);
    [consulBtn setBackgroundColor:CNavBgColor];
    [consulBtn setTitle:@"给工作人员打个分" forState:UIControlStateNormal];
    [self.contentView addSubview:consulBtn];
    [consulBtn addTarget:self action:@selector(consulBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [consulBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).mas_offset(30);
        make.left.equalTo(self.contentView).mas_offset(40);
        make.right.equalTo(self.contentView).mas_offset(-40);
        make.height.mas_offset(45);
    }];
}


#pragma mark - click
- (void)callPhoneClick {
    [DYTools callPhone: _model.phone];
}

- (void)consulBtnClick {
    HEConsulCommentViewController *vc = [HEConsulCommentViewController new];
    vc.usid = _model.service_usid;
    vc.type = HEConsulCommentTypeSickbed;
    vc.order_id = _model.order_id;
    vc.avatar = _model.avatar;
    vc.doc_name = _model.service_username;
    [vc vcBackParamBlock:^(NSNumber *scroe) {
        self.consulBtn.hidden = YES;
    }];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 布局子控件
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(ZXNavBarHeight, 0, 0, 0));
    }];
    
    // 设置参照视图的约束
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.equalTo(self.scrollView);
        make.height.mas_greaterThanOrEqualTo(kScreenHeight-ZXNavBarHeight);
    }];
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.alwaysBounceVertical = YES;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = KWhiteColor;
    }
    return _contentView;
}


@end
